function minSwapsToKeepDistance(nums, k) {
  const n = nums.length;
  const extendedNums = [...nums, ...nums]; // 复制一个周期

  // 寻找相邻红色珠子的最小距离
  let minDistance = n + 1;
  for (let i = 0; i < n; i++) {
      if (extendedNums[i] === "R" && extendedNums[i + 1] === "R") {
          minDistance = Math.min(minDistance, i + 1 + n - (i + 1));
      }
  }

  // 如果最小距离大于等于 k，无需交换
  if (minDistance >= k) {
      return 0;
  }

  let minSwaps = n; // 最多 n 次交换
  for (let i = 0; i < n; i++) {
      let count = 0;
      for (let j = 0; j < n; j++) {
          if (extendedNums[i + j] === "R") {
              count++;
          }
          if (count >= k) {
              minSwaps = Math.min(minSwaps, j);
              break;
          }
      }
  }

  return minSwaps;
}

const nums = ["R", "W", "W", "R", "R", "W", "R"];
const k = 3;
const result = minSwapsToKeepDistance(nums, k);
console.log("最小交换次数:", result);
